<template>
  <div class="app-container">
    <el-form
      :model="goods"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <div>
        <el-row>
          <el-form-item label="仓库" prop="storeId" label-width="400px">
            <el-select v-model="goods.storeId" placeholder="请选择" clearable>
              <el-option
                v-for="data in storeList"
                :key="data.id"
                :label="data.storeName"
                :value="data.id"
              />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
         <el-form-item label="出入库时间" prop="busiDate" label-width="400px"  >
          <el-date-picker clearable
          placeholder="请选择"
            v-model="goods.busiDate"
            type="date"
            value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="remark" label-width="400px" >
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="goods.remark"></el-input>
          </el-form-item>
        </el-row>

        <el-form-item>
          <el-button
            plain
            icon="el-icon-plus"
            size="minium"
            @click="handleAddStore"
            style="flex:1;margin-left:222%"
           
            v-hasPermi="['businessManagement:opInfo:add']"
          >添加明细</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div v-if="showOR">
    <table border="1" cellSpacing="0" cellPadding="0" width="900" align="center">
      <tr style="height: 50px;">
        <th>序号</th>
        <th>物品</th>
        <th>价格</th>
        <th>数量</th>
        <th>小计</th>
        <th>操作</th>
      </tr>
      <tr>
        
          <td>
            <input style="text-align:center;height: 50px;"  v-model="goods.goodsId" readonly/>
          </td>
          <td>
            <input style="text-align:center;height: 50px;" v-model="goods.goodsName" readonly/>
          </td>
          <td>
            <input style="text-align:center;height: 50px;" v-model="goods.price"/>
          </td>
          <td>
            <input style="text-align:center;height: 50px;" v-model="goods.amounts"/>
          </td>
          <td>
            <input style="text-align:center;height: 50px;" v-model="totalCapital" readonly/>
          </td>
          <td>
            <el-button type="danger"  style="width: 100px;" @click="claerGoods">删除</el-button>
          </td>
         
  
      </tr>
    </table>
  </div>
    <!-- <el-table
      border
      v-loading="loading"
      :data="goodsAll"
      align="middle"
      style="width:65%;margin-left:18%"
    >
     @selection-change="handleSelectionChange" 
      <el-table-column type="selection" width="55" align="center" /> 
      <el-table-column width="110%" label="序号" align="center" prop="goodsId" />
      <el-table-column width="210%" label="物品" align="center" prop="goodsName" />
     
      <el-table-column width="140%" label="价格" align="center" prop="price">
        <el-input  type="text" placeholder="请输入价格" />
      </el-table-column>
      <el-table-column width="140%" label="数量" align="center" prop="amounts" >
        <el-input  type="text" placeholder="请输入数量" />
      </el-table-column>
      <el-table-column width="160%" label="小计" align="center" prop="totalCapital" />

      <el-table-column
        width="180%"
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['businessManagement:opInfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table> -->
  

    <div slot="footer" class="dialog-footer" style="text-align:center;margin-top: 50px;" >
      <el-button type="primary" @click="submitForm">立即提交</el-button>
      <el-button @click="cancel">关 闭</el-button>
    </div>
  </div>
</template>

<script>
// 全局引入global


import {
  getOpInfo,
  addOpInfo,
  getBillAndOpInfo
} from "@/api/businessManagement/opInfo";
import { listStores } from "@/api/businessManagement/store";

export default {
  name: "OpInfo",
  dicts: ["bus_ment_store_type", "bus_ment_store_status"],
  data() {
    return {
      // 流水id
      billId: this.$route.query.billId,
      //默认是不展示表格
      showOR:false,
      goods: {
        remark:null,
        storeId: null,
        busiDate:null,
        // 是出库还是入库 1 为入库  2 为出库
        num: this.$route.query.num,
        // 物品id
        goodsId: this.$route.query.goodsId,
        // 物品名称
        goodsName: this.$route.query.goodsName,
        price:null,
        amounts:null,

      },
      goodsAll:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 出入库管理表格数据
      opInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeBusiDate: [],
      // 查询参数
      queryParams: {
       
        storeId: null,
        busiDate: null,
        status: null
      },
      //仓库列表
      storeList: [],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        storeId: [
          { required: true, message: "仓库id不能为空", trigger: "change" }
        ],
        busiDate: [
          { required: true, message: "出入库时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getStoreList();
    this.getList();
  },

  methods: {
   
    /** 查询正常状态的仓库 */
    getStoreList() {
      
      this.loading = true;
      listStores().then(response => {
        this.storeList = response.data;
        this.loading = false;
      });
    },

    /** 查询出入库管理列表 */
    getList() {
      if(this.billId){
      // 根据入库单据id 查明细
      getBillAndOpInfo(this.billId).then(resp => {
              /* todo */
              console.log(resp.data);
                this.goods = resp.data;
                this.showOR = true;
            })
      }
      
     

      if(this.goods.goodsId) {
        this.showOR = true;
      }

      // this.loading = true;
      // this.queryParams.params = {};
      // if (null != this.daterangeBusiDate && "" != this.daterangeBusiDate) {
      //   this.queryParams.params["beginBusiDate"] = this.daterangeBusiDate[0];
      //   this.queryParams.params["endBusiDate"] = this.daterangeBusiDate[1];
      // }
      // listOpInfo(this.queryParams).then(response => {
      //   this.opInfoList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
      /**联表查询 */
      // listOpInfoAndListInfo(this.queryParams).then(response => {
      //   this.opInfoList = response.rows;
      //   this.total = response.total;
      //   this.loading = false;
      // });
    },
    // 关闭按钮
    cancel() {
      window.close();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        storeId: null,
        busiDate: null,
        status: null,
        operateDate: null,
        operatorId: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeBusiDate = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 添加明细 */
    handleAddStore() {
      this.$router.push("/businessManagement/goodsFlag?flag=1&num="+this.goods.num);
      /* let categoryPage = this.$router.push("/businessManagement/goodsFlag?flag=1");
      window.open(
        categoryPage.href,
        "_blank",
        "height=600, width=1200,top=150,left=500"
      ); */
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getOpInfo(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改出入库管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
  
          
          console.log(this.goods);
          addOpInfo(this.goods).then(response => {
              window.close();
            });
           
       
    },
    
    claerGoods() {
      
      this.showOR = false;
    }
  },
  computed: {
    totalCapital () {
        return this.goods.price * this.goods.amounts;
    },
    
}

};
</script>
